#ifndef SMVPROCESS_H
#define SMVPROCESS_H
#include <QThread>

class smvProcess : public QThread
{
    Q_OBJECT

public:
    smvProcess();
    ~smvProcess();

    void run();
    void stop(){stopFlag = true;}

    enum _run_mode{
        NOOP,
        GET_DEVICE_LIST,
        POWER_BASICS,
        TIME,
    } run_mode, seted_mode;

    typedef struct{
        bool	isEmpty; // default 0, set it 1 when we find the first device and 2 for second...
        unsigned char	dest_addr[6];
        unsigned int	appid;
        unsigned int	smp_rate;	//in k bits

        unsigned int	packets_cnt;
    } dut;

    bool isOpen(){return isOpened;}

    typedef QList<double> samplesBuff;

    double mu_PointArray1[6][10*256];
    double mu_PointArray2[6][10*256];

    double (*mu_sampleArray)[6][10*256];
    double (*mu_showArray)[6][10*256];

    double rms_Ua;
    double rms_Ub;
    double rms_Uc;
    double rms_Ia;
    double rms_Ib;
    double rms_Ic;

    bool stopFlag;

    double dbl_frequency_Ua;
    double dbl_frequency_Ub;
    double dbl_frequency_Uc;

    double dbl_phase_Ua;
    double dbl_phase_Ub;
    double dbl_phase_Uc;
    double dbl_phase_Ia;
    double dbl_phase_Ib;
    double dbl_phase_Ic;

    double dbl_frequency_tmp;
    double dbl_rms;
    double dbl_phase;

    double dc_level[6];

private:
    bool flagClose;
    bool isFirstCycle;
    double digiFiter_dcBlocking(int flag, double newComing);

    double lastInput[9];
    double lastOutput[9];

    double rms_Ua_tmp;
    double rms_Ub_tmp;
    double rms_Uc_tmp;
    double rms_Ia_tmp;
    double rms_Ib_tmp;
    double rms_Ic_tmp;

    bool isOpened;
    bool reOpen;
    int channelList[6];

    void init();

    bool openEthSocket(int flag);
    void closeEthSocket();
    void reOpenEthSocket(_run_mode seted_mode);
    void setChannelMapping();
    bool setSmvPara(const unsigned char *buffer, int len);
    void listAllDevices();
    void harmonic(double *s,int M,int len,int fs);
    void harmonic(double *s,int M,int len);

    QString deviceAddr;
    QString appID;
    QString samplesCnt;

    int smvASDUCnt;
    int smvASDULength;

    int smvSmpCntLength;
    int smvSmpCntStart;
    int smvFirstSmpCntStart;

    int smvNumOfChannels;
    int smvPosOfFirstSample;

    bool smvSyncStatus;

    dut	dut_lists[10];	// list ten devices at most

    void findoutSamples();
    void timeAnalyzer();

public:
    typedef enum
    {
        Ua,
        Ub,
        Uc,
        Ia,
        Ib,
        Ic
    } channelName;

    void setChannel(channelName name, int channel) {channelList[name] = channel;}
    void setReopen(_run_mode mode){reOpen = true; seted_mode = mode;}
    void setClose(){flagClose = true;}

    typedef struct _packet_error           //延时间隔时间
    {
        char	   de_name[6];
        unsigned short    flag;   //'a'
        long unsigned int	error_1;	// error less than 1us
        long unsigned int	error_2;	// error between 1~2 us
        long unsigned int	error_4;	// error between 2~3 us
        long unsigned int	error_6;	// error between 3~4 us
        long unsigned int	total;
        long unsigned int	lost;
        long unsigned int	disorder;
        long unsigned int   delay;
        long unsigned int   delay_defined;
    }packet_error;

    packet_error test_result;
    packet_error test_result_tmp;

signals:
    void sigFind(QString,QString,QString,int,int);
    void samplesIsReady();
};

#endif // SMVPROCESS_H
